﻿using System;
using System.Diagnostics;
using System.IO;
using Accountant.DataModel;
using ApprovalTests;
using ApprovalTests.Reporters;
using ApprovalUtilities.Utilities;
using NUnit.Framework;
using OtdrDataFormat.UnitTests;

namespace UnitTests
{
  [TestFixture, UseReporter(typeof(AraxisReporter))]
  public class MySerializer
  {
    [Test]
    public void Initializer()
    {
      var ctx = new AccountantContext { Database = DefaultDatabase() };
      ctx.Load();
      Approvals.Verify(ctx.Dump());
    }
    [Test]
    public void RandomDataGenerator()
    {
      var ctx = new AccountantContext { Database = DefaultDatabase() };
      ctx.Load();
      var g = new RandomDataGenerator(ctx, 0);
      g.Run(20, new DateTime(2000, 5, 12));
      ctx.SaveChanges();
      Approvals.Verify(ctx.Dump());
    }
    [Test]
    public void Load()
    {
      var ctx = new AccountantContext { Database = 
        new Database(Rooted("InitializerAndRandomTransactions")) };
      ctx.Load();
      Approvals.Verify(ctx.Dump());
    }
    [Test, Ignore]
    public void PerfTest()
    {
      var database = DefaultDatabase();

      var ctx = new AccountantContext { Database = database };
      ctx.Load();
      var g = new RandomDataGenerator(ctx, 0);
      g.Run(2000000, new DateTime(2000, 5, 12));
      
      var stopwatch = Stopwatch.StartNew();
      ctx.SaveChanges();
      stopwatch.Stop();
      Console.WriteLine("Write time: " + stopwatch.Elapsed);

      Console.WriteLine("Size: " + ctx.Database.GetSize());

      var ctx2 = new AccountantContext { Database = database };
      var stopwatch2 = Stopwatch.StartNew();
      ctx2.Load(); 
      stopwatch2.Stop();
      Console.WriteLine("Read time: " + stopwatch2.Elapsed);
    }
  
    private static Database DefaultDatabase()
    {
      var tempFileName = Path.GetTempFileName();
      File.Delete(tempFileName);
      var defaultDatabase = new Database(tempFileName);
      defaultDatabase.SetInitializer(new DbInitializer());
      return defaultDatabase;
    }
    private static string Rooted(string file)
    {
      return Path.Combine(PathUtilities.GetDirectoryForCaller(0), file);
    }
  }
}
